﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary.App_Code;

/// <summary>
/// Summary description for ItemDAL
/// </summary>
public class ItemDAL
{
    public static ShopMobileDataContext data = new ShopMobileDataContext();
	public ItemDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static List<ItemBLL> getAllItems_Cat()
    {
        var ds = from it in data.Items
                 where it.stt == 1
                 orderby it.id descending
                 select new ItemBLL(it.id,it.name,it.Category.name_cat,it.Category.id,it.price.Value, it.quantity_id.Value,it.image,it.image2, it.display,it.camera,it.configuration,it.connectivity,it.datatranfer,it.generalfeatures);
        return ds.ToList<ItemBLL>();
    } 
    public static byte[] getImage(string ID)
    {
        int id=int.Parse(ID);
        var info = (from im in data.Items
                   where im.id == id
                   select im).First();
        return info.image2;
    }
      
    public static List<ItemBLL> getItems_Cat(int _id_cat)
    {
        var ds = (from it in data.Items
                 where it.id_cat == _id_cat && it.stt == 1
                 select new ItemBLL(it.id, it.name, it.Category.id, it.price.Value,it.image,it.Category.name_cat)).Skip(0).Take(9);
        return ds.ToList<ItemBLL>();
    }
    public static List<ItemBLL> getItem_Details(int id)
    {
        var ds = (from it in data.Items
                  where it.id == id
                  select new ItemBLL(it.id, it.name, it.Category.name_cat, it.Category.id, it.price.Value, it.quantity_id.Value, it.image, it.image2, it.display, it.camera, it.configuration, it.connectivity, it.datatranfer, it.generalfeatures));
        return ds.ToList<ItemBLL>();
    }

    public static List<ItemBLL> getItemInCart(string id)
    {
        string[] cart = id.Split(',');
        var ds = from it in data.Items
                 where cart.Contains(it.id.ToString())
                 select new ItemBLL(it.id,it.name,it.Category.name_cat,it.Category.id,it.price.Value, it.quantity_id.Value,it.image,it.image2, it.display,it.camera,it.configuration,it.connectivity,it.datatranfer,it.generalfeatures);
        return ds.ToList<ItemBLL>();
    }
    public static bool checkItemAlive(int item_id)
    {
        var check = from it in data.Items
                    where it.id == item_id &&it.Items_Quantity.id==it.quantity_id
                    select it.Items_Quantity.admin_hide;

        if (check.Count() > 0)
        {
            if (check.First() == 0)
                return true;
        }
        return false;
    }
    public static int getCash(string id)
    {
        var cash = from it in data.Items
                   where it.id.ToString() == id
                   select it.price;
        return int.Parse(cash.First().ToString());
    }

    public static List<ItemBLL> getSearch(string name)
    {
        var ds = (from it in data.Items
                  where it.name.Contains(name)
                  orderby it.id descending
                  select new ItemBLL(it.id, it.name, it.Category.name_cat, it.Category.id, it.price.Value, it.quantity_id.Value, it.image, it.image2, it.display, it.camera, it.configuration, it.connectivity, it.datatranfer, it.generalfeatures));
        return ds.ToList<ItemBLL>();
    }
    public static List<ItemBLL> getAllCate()
    {
        var ds = (from it in data.Items
                  orderby it.id descending
                  select new ItemBLL(it.id,it.name,it.Category.name_cat,it.Category.id,it.price.Value, it.quantity_id.Value,it.image,it.image2, it.display,it.camera,it.configuration,it.connectivity,it.datatranfer,it.generalfeatures));

        return ds.ToList<ItemBLL>();
    }

    public static bool addItem(ItemBLL it)
    {
        Item i = new Item();
        if (i.name == it.name)
        {
            return false;
        }
        else
        {
            i.name = it.name;
            i.id_cat = 1; //CategoryDAL.getID(it.name_cat);
            i.image = it.image;
            i.image2 = it.image2;
            i.quantity_id = it.quantity_id;
            i.price = it.price;
            i.sale_off = it.sale_off;
            i.stt = it.stt;
            i.display = it.display;
            i.camera = it.camera;
            i.configuration = it.configuration;
            i.connectivity = it.connectivity;
            i.datatranfer = it.datatranfers;
            i.generalfeatures = it.generalfeatures;
            i.stt = 0;
            data.Items.InsertOnSubmit(i);
            data.SubmitChanges();
            return true;
        }

    }

    public static bool editItem(ItemBLL it)
    {
        try
        {
            Item i = data.Items.Single(its => its.id == it.id);
            i.name = it.name;
            //i.id_cat = CategoryDAL.getID(it.name_cat);
            i.image = it.image;
            i.image2 = it.image2;
            i.quantity_id = it.quantity_id;
            i.price = it.price;
            i.sale_off = it.sale_off;
            i.stt = it.stt;
            i.display = it.display;
            i.camera = it.camera;
            i.configuration = it.configuration;
            i.connectivity = it.connectivity;
            i.datatranfer = it.datatranfers;
            i.generalfeatures = it.generalfeatures;
            i.stt = it.stt;
            data.SubmitChanges();
            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    public static bool delItem(int id)
    {
        try
        {
            Item it = data.Items.Single(i => i.id == id);
            data.Items.DeleteOnSubmit(it);
            data.SubmitChanges();
            return true;
        }
        catch (Exception)
        {
            
            return false;
        }
    }

    public static int totalItem()
    {
        var car = from c in data.Items

                  select c.id;
        return car.Count();
    }
}



        